package com.higgins.eventtracking.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.LineChart;
import com.google.gwt.visualization.client.visualizations.LineChart.Options;

@SuppressWarnings("deprecation")
public class WeightChart implements EntryPoint {
	private LineChart line = null;
	private final ListBox monthlyOrWeeklyListBox = new ListBox();
	private final ListBox chartTypeListBox = new ListBox();
	private final Label errorLabelContainer = new Label();
	private final EventTrackingServiceAsync greetingService = GWT
	.create(EventTrackingService.class);
	private Runnable onLoadCallback ;

	@Override
	public void onModuleLoad() {
		attachComponents();
		populateMonthlyOrWeeklyListBox();
		populateChartTypeList();
		executeChart();
	}
	private void attachComponents()	{
		RootPanel.get("weeklyOrMonthlyListBox").add(monthlyOrWeeklyListBox);
		RootPanel.get("chartTypeListBox").add(chartTypeListBox);
		RootPanel.get("errorLabelContainer").add(errorLabelContainer);
	}
	private void populateMonthlyOrWeeklyListBox()	{
		monthlyOrWeeklyListBox.addItem("Monthly");
		monthlyOrWeeklyListBox.addItem("Weekly");
		monthlyOrWeeklyListBox.addChangeHandler(new WeightChartHandler());		
	}
	private void populateChartTypeList()	{
		chartTypeListBox.addItem("Weight");
		chartTypeListBox.addItem("Fat");
		chartTypeListBox.addItem("Water");		
		chartTypeListBox.addItem("Muscle");
		chartTypeListBox.addItem("FatCalipers");
		chartTypeListBox.addChangeHandler(new WeightChartHandler());		
	}
	private void executeChart()	{
		  int selectedItem = chartTypeListBox.getSelectedIndex();
		  String exercise = chartTypeListBox.getItemText(selectedItem);

		  selectedItem = monthlyOrWeeklyListBox.getSelectedIndex();
		  String monthlyOrWeekly = monthlyOrWeeklyListBox.getItemText(selectedItem);
		
		  greetingService.retrieveWeightChartData(exercise, monthlyOrWeekly.equals("Monthly"),
				new AsyncCallback<List<Double>>() {
					public void onFailure(Throwable caught) {
						errorLabelContainer.setText("Error Caught: " + caught.toString());
					}
					public void onSuccess(final List<Double> results) {
						if (results!=null)
							errorLabelContainer.setText("Successfully Loaded");
						runCharts(results);
					}
				});
	}
	
	private void updateChart()	{
		  int selectedItem = chartTypeListBox.getSelectedIndex();
		  String chartType = chartTypeListBox.getItemText(selectedItem);

		  selectedItem = monthlyOrWeeklyListBox.getSelectedIndex();
		  String monthlyOrWeekly = monthlyOrWeeklyListBox.getItemText(selectedItem);
		
		  greetingService.retrieveWeightChartData(chartType, monthlyOrWeekly.equals("Monthly"),
				new AsyncCallback<List<Double>>() {
					public void onFailure(Throwable caught) {
						errorLabelContainer.setText("Error Caught: " + caught.toString());
					}
					public void onSuccess(final List<Double> results) {
						if (results!=null)
							errorLabelContainer.setText("Successfully Loaded");
						line.draw(createTable(results), createOptions());
					}
				});
	}
		
	 private void runCharts(final List<Double> results)	{
       	onLoadCallback = new Runnable() {
	        public void run() {
	        	Panel panel = RootPanel.get("chart");
	        	line = new LineChart(createTable(results), createOptions());
	        	panel.add(line);
	        }
	      };
		
		VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE);
	 }
		
		
	  private Options createOptions() {
		  int selectedItem = chartTypeListBox.getSelectedIndex();
		  String title = chartTypeListBox.getItemText(selectedItem);
		  Options options = Options.create();
		  options.setWidth(600);
		  options.setHeight(400);
		  options.setTitle(title);
		  options.setTitleX("Week");
		  options.setTitleY(title);
		  return options;
	  }

	  private DataTable createTable(List<Double> items) {
		  	int selectedItem = chartTypeListBox.getSelectedIndex();
		  	String chartType = chartTypeListBox.getItemText(selectedItem);
		    DataTable data = DataTable.create();
		    data.addColumn(ColumnType.NUMBER, chartType);
		    
		    data.addRows(items.size());
		    int count = 0;
		    for (Double item:items)	{
		        data.setValue(count, 0, item);
		        count++;
		    }
	    return data;
	  }
		  
	class WeightChartHandler implements ChangeHandler {
		public void onChange(ChangeEvent event) {			
			updateChart();
		}
	}		  
}
